
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: Block
 * Package: PACKAGE_NAME
 * @Author: shangyuan_wu
 * @Create: 2022/12/15 - 14:19
 * Description:
 * @see Block 迷宫单元类
 * @see #i 二维数组的下标i，代表第i-1行Block
 * @see #j 二维数组的下标j，代表第j-1行Block
 * @see #h 宽高
 * @see #x1 第一个顶点的x坐标
 * @see #x2 第二个顶点的x坐标
 * @see #x3 第三个顶点的x坐标
 * @see #x4 第四个顶点的x坐标
 * @see #y1 第一个顶点的y坐标
 * @see #y2 第二个顶点的y坐标
 * @see #y3 第三个顶点的y坐标
 * @see #y4 第四个顶点的y坐标
 * @see #walls 上下左右4个墙是否显示,true：显示，false：隐藏
 * @see #visited 是否被访问
 * @see #getNeighbors(int, Block[][], boolean) 用于获取指定方向上有没有未访问过的Block，并且返回一个Block
 * @see #findNeighbors(Block[][]) 用于获取所有方向上未访问的Block，并返回一个Block的集合
 * @see #draw(Graphics) 用于画 "墙"
 */
public class Block {
    private int i = 0;
    private int j = 0;
    private int h = 0;
    private int x1 = 0;
    private int x2 = 0;
    private int x3 = 0;
    private int x4 = 0;
    private int y1 = 0;
    private int y2 = 0;
    private int y3 = 0;
    private int y4 = 0;
    boolean[] walls = new boolean[4];
    private boolean visited = false;
    private Color color = new Color(0, 0, 0 ,0);

    public Block(int i, int j, int h) {
        this.i = i;
        this.j = j;
        this.h = h;
//        this.panel = panel;
        //左上角坐标
        this.x1 = Constant.START + j * h;
        this.y1 = Constant.START + i * h;
        //右上角坐标
        this.x2 = Constant.START + (j + 1) * h;
        this.y2 = Constant.START + i * h;
        //右下角坐标Constant.
        this.x3 = Constant.START + (j + 1) * h;
        this.y3 = Constant.START + (i + 1) * h;
        //左下角坐标Constant.
        this.x4 = Constant.START + j * h;
        this.y4 = Constant.START + (i + 1) * h;
        //默认上下左右4个墙都显示
        walls[Constant.TOP] = true;
        walls[Constant.RIGHT] = true;
        walls[Constant.BOTTOM] = true;
        walls[Constant.LEFT] = true;
    }

    public Block() {

    }

    public Block(int i, int j, int h, int x1, int x2, int x3, int x4, int y1, int y2, int y3, int y4, boolean[] walls, boolean visited) {
        this.i = i;
        this.j = j;
        this.h = h;
        this.x1 = x1;
        this.x2 = x2;
        this.x3 = x3;
        this.x4 = x4;
        this.y1 = y1;
        this.y2 = y2;
        this.y3 = y3;
        this.y4 = y4;
        this.walls = walls;
        this.visited = visited;
    }

    public Block getNeighbors(int type, Block[][] blocks, boolean ignoreVisit) {
        //用于临时记录行数和列数
        int ti = 0;
        int tj = 0;
        //上边
        if (type == Constant.TOP) {
            ti = i - 1;
            tj = j;
        }
        //右边
        if (type == Constant.RIGHT) {
            ti = i;
            tj = j + 1;
        }
        //下边
        if (type == Constant.BOTTOM) {
            ti = i + 1;
            tj = j;
        }
        //左边
        if (type == Constant.LEFT) {
            ti = i;
            tj = j - 1;
        }
        //创建一个临时的Block的变量，当作返回值
        Block neighbor;
        //超出边界
        if (ti < 0 || tj < 0 || ti >= Constant.ROWS || tj >= Constant.COLS) {
            neighbor = null;
        } else if (blocks[ti][tj].visited && !ignoreVisit) {
            //已经访问过了
            neighbor = null;
        } else {
            //将这个block值复制给neighbor
            neighbor = blocks[ti][tj];
        }
        return neighbor;
    }

    public List<Block> findNeighbors(Block[][] blocks) {
        //创建一个集合用作返回值
        List<Block> blockList = new ArrayList<>();
        Block top = this.getNeighbors(Constant.TOP, blocks, false);
        Block right = this.getNeighbors(Constant.RIGHT, blocks, false);
        Block bottom = this.getNeighbors(Constant.BOTTOM, blocks, false);
        Block left = this.getNeighbors(Constant.LEFT, blocks, false);
        if (top != null) {
            blockList.add(top);
        }
        if (right != null) {
            blockList.add(right);
        }
        if (bottom != null) {
            blockList.add(bottom);
        }
        if (left != null) {
            blockList.add(left);
        }
        return blockList;
    }

    public void draw(Graphics g) {
        //判断上、右、下、左 的墙，true的话墙就会有，否则墙就没有
        //绘制上墙
        if (walls[Constant.TOP]) {
            g.drawLine(x1, y1, x2, y2);
        }
        //绘制右墙
        if (walls[Constant.RIGHT]) {
            g.drawLine(x2, y2, x3, y3);
        }
        //绘制下墙
        if (walls[Constant.BOTTOM]) {
            g.drawLine(x3, y3, x4, y4);
        }
        //绘制左墙
        if (walls[Constant.LEFT]) {
            g.drawLine(x4, y4, x1, y1);
        }

        Color oColor = g.getColor();

        g.setColor(color);
        g.fillRect(x1, y1, h - 3, h - 3);
        g.setColor(oColor);
    }


    public void setColor(Color color){
        this.color = color;
    }

    /**
     * 获取
     * @return i
     */
    public int getI() {
        return i;
    }

    /**
     * 设置
     * @param i
     */
    public void setI(int i) {
        this.i = i;
    }

    /**
     * 获取
     * @return j
     */
    public int getJ() {
        return j;
    }

    /**
     * 设置
     * @param j
     */
    public void setJ(int j) {
        this.j = j;
    }

    /**
     * 获取
     * @return h
     */
    public int getH() {
        return h;
    }

    /**
     * 设置
     * @param h
     */
    public void setH(int h) {
        this.h = h;
    }

    /**
     * 获取
     * @return x1
     */
    public int getX1() {
        return x1;
    }

    /**
     * 设置
     * @param x1
     */
    public void setX1(int x1) {
        this.x1 = x1;
    }

    /**
     * 获取
     * @return x2
     */
    public int getX2() {
        return x2;
    }

    /**
     * 设置
     * @param x2
     */
    public void setX2(int x2) {
        this.x2 = x2;
    }

    /**
     * 获取
     * @return x3
     */
    public int getX3() {
        return x3;
    }

    /**
     * 设置
     * @param x3
     */
    public void setX3(int x3) {
        this.x3 = x3;
    }

    /**
     * 获取
     * @return x4
     */
    public int getX4() {
        return x4;
    }

    /**
     * 设置
     * @param x4
     */
    public void setX4(int x4) {
        this.x4 = x4;
    }

    /**
     * 获取
     * @return y1
     */
    public int getY1() {
        return y1;
    }

    /**
     * 设置
     * @param y1
     */
    public void setY1(int y1) {
        this.y1 = y1;
    }

    /**
     * 获取
     * @return y2
     */
    public int getY2() {
        return y2;
    }

    /**
     * 设置
     * @param y2
     */
    public void setY2(int y2) {
        this.y2 = y2;
    }

    /**
     * 获取
     * @return y3
     */
    public int getY3() {
        return y3;
    }

    /**
     * 设置
     * @param y3
     */
    public void setY3(int y3) {
        this.y3 = y3;
    }

    /**
     * 获取
     * @return y4
     */
    public int getY4() {
        return y4;
    }

    /**
     * 设置
     * @param y4
     */
    public void setY4(int y4) {
        this.y4 = y4;
    }

    /**
     * 获取
     * @return walls
     */
    public boolean[] getWalls() {
        return walls;
    }

    /**
     * 设置
     * @param walls
     */
    public void setWalls(boolean[] walls) {
        this.walls = walls;
    }

    /**
     * 获取
     * @return visited
     */
    public boolean isVisited() {
        return visited;
    }

    /**
     * 设置
     * @param visited
     */
    public void setVisited(boolean visited) {
        this.visited = visited;
    }

    public String toString() {
        return "Block{i = " + i + ", j = " + j + ", h = " + h + ", x1 = " + x1 + ", x2 = " + x2 + ", x3 = " + x3 + ", x4 = " + x4 + ", y1 = " + y1 + ", y2 = " + y2 + ", y3 = " + y3 + ", y4 = " + y4 + ", walls = " + walls + ", visited = " + visited + "}";
    }
}